#functions
function ZipFiles( $zipfilename, $sourcedir ){
    [Reflection.Assembly]::LoadWithPartialName( "System.IO.Compression.FileSystem" ) > $null;
    $compressionLevel = [System.IO.Compression.CompressionLevel]::Optimal
    $sourcedir = (Get-Item $sourcedir).FullName;
    [System.IO.Compression.ZipFile]::CreateFromDirectory( $sourcedir, $zipfilename, $compressionLevel, $false ) > $null;
}


cls;
# Parameters
$build = "Release";
$dst = "C:\workspace\ProjectAlbatross.Net\Deployment";
$workspace = "C:\Workspace\ProjectAlbatross.NET";
set-alias msbuild C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe


#delete the old build and recreate the directories
if (Test-Path $dst){ Remove-Item $dst -Recurse -Force -Confirm:$false; }
New-Item $dst\src -ItemType directory -Force > $nul;


#copy everything over first
copy-item $workspace\Albatross.WPF.Common -Destination $dst\src -Recurse;
copy-item $workspace\Albatross.Shell -Destination $dst\src -Recurse;
copy-item $workspace\Albatross.SampleApp -Destination $dst\src -Recurse;
copy-item $workspace\Albatross.SSRSViewer -Destination $dst\src -Recurse;
Copy-Item $workspace\packages -Destination $dst\src -Recurse
copy-item $workspace\Albatross.Shell.sln $dst\src

#clean up the folders
Get-ChildItem -Path $dst\src -Recurse -Directory | ForEach-Object{ if($_.Name -eq "obj" -or $_.Name -eq "bin") { remove-item $_.FullName -Force -Recurse -Confirm:$false;} };
Get-ChildItem -Path $dst\src\*.user -Recurse -file | ForEach-Object{ remove-item $_.FullName -Confirm:$false -Force;}
Get-ChildItem -Path $dst\src\*.vspscc -Recurse -file | ForEach-Object{ remove-item $_.FullName -Confirm:$false -Force;}
Get-ChildItem -Path $dst\src\*.vssscc -Recurse -file | ForEach-Object{ remove-item $_.FullName -Confirm:$false -Force;}


#build the project
msbuild $dst\src\Albatross.Shell.sln /p:Configuration=$build


#copy the binary
copy-Item $dst\src\Albatross.Shell\bin\$build -Destination $dst\binary\AlbatrossShell -Force -Recurse
copy-Item $dst\src\Albatross.SampleApp\bin\$build -Destination $dst\binary\SampleApp -Force -Recurse;

#clean up the folders one more time
Get-ChildItem -Path $dst\src -Recurse -Directory | ForEach-Object{ if($_.Name -eq "obj" -or $_.Name -eq "bin") { remove-item $_.FullName -Force -Recurse -Confirm:$false;} };
Get-ChildItem -Path $dst\src\*.user -Recurse -file | ForEach-Object{ remove-item $_.FullName -Confirm:$false -Force;}
Get-ChildItem -Path $dst\src\*.vspscc -Recurse -file | ForEach-Object{ remove-item $_.FullName -Confirm:$false -Force;}
Get-ChildItem -Path $dst\src\*.vssscc -Recurse -file | ForEach-Object{ remove-item $_.FullName -Confirm:$false -Force;}

#find the version number
$asm = [System.Reflection.Assembly]::ReflectionOnlyLoadFrom("$dst\binary\AlbatrossShell\Albatross.Shell.exe");
$version = $asm.GetName().Version.ToString();


#zip it!
$zip = "$dst\..\Albatross.Shell_V$version.zip";
if(Test-Path $zip){
    Remove-Item $zip -Force;
}
zipfiles -zipfilename "$dst\..\Albatross.Shell_V$version.zip" -sourcedir $dst;